<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2023/10/24
  Time: 9:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>网上调查步骤之三</title>
</head>
<body>
<%@include file="top.html"%>
<%
    //获取用户姓名和性别
    String namesex = (String)session.getValue("namesex");
    String hobby[] = new String[5];
    //统计用户选择后的信息
    for(int i=0;i<hobby.length;i++){
        //hobby数组中存储了受访者的选择
        String param = "hobby"+i;
        String getparam = request.getParameter(param);
        if(getparam != null){
            //转换编码方式
            hobby[i] = new String(getparam.getBytes("ISO-8859-1"), "utf-8");
            //添加synchronized同步锁，方式出现读写混乱
            synchronized (application){
                //获取爱好信息
                Integer count = (Integer)application.getAttribute(param);
                if(count == null)//第一次设置该参数进行初始化
                    count = new Integer(0);
                count = new Integer(count.intValue()+1);
                //将统计信息写会application
                application.setAttribute(param, count);
            }
        }
    }
%>
<p>
    <%=(String)session.getValue("namesex")%>,你所感兴趣的业余爱好如下：
</p>
<%
    //显示当前用户的爱好信息
    for(int i=0;i<hobby.length;i++) {
        if (hobby[i] != null) {
            out.print("<p>");
            out.print(hobby[i]);
            out.print("</p>");
        }
    }
    //显示所有用户的爱好信息
    String hobbyName[] = {"电影欣赏","阅读书籍","体育运动","户外旅游","流行音乐"};
    for(int i=0;i<hobby.length;i++){
        Integer count = (Integer)application.getAttribute("hobby"+i);
        if(count==null)
            count = new Integer(0);
        out.println("<p>选择"+hobbyName[i]+"的人次为"+count+"</p>");
    }
%>
</body>
</html>
